package com.shouhuobao.samecity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.SameCityHistoryBean;
import com.collectplus.express.model.SameCityParcelBean;

/**
 * Created by joker on 2018/1/15.
 * 同城收件人界面，展示历史记录和联想界面
 */
public class SameCityReceiverAddressActivity extends SameCityBaseActivityWithSearch implements View.OnClickListener {

    private ListView mListView;// 收件人历史列表
    private SameCityAddressHistoryAdapter mAdapter;//历史记录适配起

    private boolean isFrom = false;//true 从SameCityInfoConfirmActivity启动当前类，进入当前界面
    private int type;// 1 从计算价格界面进入

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFrom = getIntent().getBooleanExtra("isFrom", false);
        type = getIntent().getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
        //收件人历史记录
        showLoadingDialog(null);
        AppHttp.getInstance().getAddressHistorySameCity(2);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.same_city_receiver_address);
        super.findViewById();
        getAppTitle().setCommonTitle("收件地址");
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new SameCityAddressHistoryAdapter(null, getContext());
        mListView.setAdapter(mAdapter);

//        mEditInput.addTextChangedListener(new TextWatcherExt() {
//            private Handler handler = new Handler() {
//                @Override
//                public void handleMessage(Message msg) {
//                    if (msg.what == 99999) {
//                        String keywords = mEditInput.getText().toString();
//                        if (isEmpty(keywords)) {
//                            // ignore
//                        } else {
//                            autoSearch = true;
//                            if (isNotEmpty(mCurrentCityName)) {
//                                showLoadingDialog(null);
//                                AppHttp.getInstance().serachAddressByKeyWords(mCurrentCityName, keywords);
//                            }
//                        }
//                    }
//                }
//            };
//
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                handler.removeMessages(99999);
//                handler.sendEmptyMessageDelayed(99999, 1000);
//            }
//        });
//        mEditInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                //
//                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//                    String keywords = mEditInput.getText().toString();
//                    if (isEmpty(keywords)) {
//                        // ignore
//                    } else {
//                        autoSearch = false;
//                        if (isNotEmpty(mCurrentCityName)) {
//                            showLoadingDialog(null);
//                            AppHttp.getInstance().serachAddressByKeyWords(mCurrentCityName, keywords);
//                        }
//                    }
//                    return true;
//                }
//                return false;
//            }
//        });
        //历史记录进行点击选择，返回上一页
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SameCityHistoryBean item = (SameCityHistoryBean) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("item", item);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    SameCityParcelBean bean = AppCache.getSameCityParcelBean();
                    if (bean == null) {
                        bean = new SameCityParcelBean();
                    }
                    bean.setReceiveBuildName(item.getBuildName());
                    bean.setReceiveAddress(item.getAddress());
                    bean.setReceiveName(item.getUserName());
                    bean.setReceivePhone(item.getPhone());
                    bean.setReceiveLatitude(item.getLatitude());
                    bean.setReceiveLongitude(item.getLongitude());
                    bean.setReceiveProvince(item.getProvince());
                    bean.setReceiveCity(item.getCity());
                    bean.setReceiveDistrict(item.getDistrict());
                    bean.setReceiveAddressDesc(item.getAddressDesc());
                    AppCache.setSameCityParcelBean(bean);
                    if (isEmpty(item.getAddressDesc())) {
                        if (isFrom) {
                            Intent intent = new Intent();
                            intent.putExtra("finish", false);
                            intent.putExtra("item", item);
                            setResult(RESULT_OK, intent);
                            finish();
                        } else {
                            Intent intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                            intent.putExtra("type", 1);
                            intent.putExtra("loadType", 0);
                            intent.putExtra("isFrom", true);
                            startActivityForResult(intent, 1602);
                        }
                    } else {
                        if (isFrom) {
                            Intent intent = new Intent();
                            intent.putExtra("finish", true);
                            setResult(RESULT_OK, intent);
                        } else {
                            setResult(RESULT_OK);
                        }
                        finish();
                    }
                }
            }
        });
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SameCityHistoryBean item = (SameCityHistoryBean) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
                // 切换视图
                onClick(mSearchOrCancel);
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("item", item);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    //点击跳转到确认界面，进行填写剩余的信息
                    if (isFrom) {
                        Intent intent = new Intent();
                        intent.putExtra("item", item);
                        intent.putExtra("finish", false);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                        intent.putExtra("type", 1);
                        intent.putExtra("item", item);
                        intent.putExtra("loadType", 0);
                        startActivityForResult(intent, 1203);
                    }
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //收件人历史记录
            case MsgID.get_address_history_same_city:
                cancelLoadingDialog();
                final AppResult<SameCityHistoryBean> result = JSON.parseList(msg.obj, SameCityHistoryBean.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mAdapter != null) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter = new SameCityAddressHistoryAdapter(result.getResultList(), getContext());
                                mListView.setAdapter(mAdapter);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            //根据关键字搜索附近位置
//            case MsgID.samecity_address_by_keywords:
//                cancelLoadingDialog();
//                final AppResult<SameCityHistoryBean> serachResult = JSON.parseList(msg.obj, SameCityHistoryBean.class);
//                if (serachResult.isSuccess()) {
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (serachResult.getResultList() != null && serachResult.getResultList().size() > 0) {
//                                mAdapter2 = new SameCityAddressAdapter(serachResult.getResultList(), getContext());
//                                mListView2.setAdapter(mAdapter2);
//
//                                if (autoSearch) {
//                                    // 通过联想搜索 不收键盘
//                                } else {// 点击搜索 收起键盘
////                                    getHandler().postDelayed(new Runnable() {
////                                        @Override
////                                        public void run() {
////                                            ImmUtils.hide(getContext());
////                                        }
////                                    }, 200);
//                                }
//                            } else {
//                                mAdapter2 = new SameCityAddressAdapter(null, getContext());
//                                mListView2.setAdapter(mAdapter2);
//                                showToast("搜索结果为空");
//                            }
//                        }
//
//                    });
//
//                }
//                return false;
            default:
                return super.handleMessage(msg);
        }

    }

//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            // 搜索框
//            case R.id.visit_edit_input:
//                // 非"搜索"状态下点击搜索框--切换到搜索状态
//                if (mSearchOrCancel.getTag() == null) {
//                    mSearchOrCancel.setVisibility(View.VISIBLE);
//                    mSearchOrCancel.setTag("");//
//                    mSearchOrCancel.setText("取消");
//                    mTitleLayout.setVisibility(View.GONE);
//                    mListView2.setVisibility(View.VISIBLE);
//                    getHandler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            mEditInput.setFocusable(true);
//                            mEditInput.setFocusableInTouchMode(true);
//                            mEditInput.requestFocus();
//                            Android.showSoftInput(getContext(), mEditInput);
//                        }
//                    }, 200);
//                } else {
//                    // 搜索状态点击, 不作处理
//                }
//                break;
//            // 搜索
//            case R.id.visit_search_submit:
//                if (mSearchOrCancel.getTag() == null) {
//                    // 非搜索状态, 点击搜索按钮不做处理
//                } else {
//                    // 取消
//                    mSearchOrCancel.setTag(null);//
//                    mSearchOrCancel.setText("搜索");
//                    mSearchOrCancel.setVisibility(View.GONE);
//                    mTitleLayout.setVisibility(View.VISIBLE);
//                    mListView2.setVisibility(View.GONE);
//                    mListView2.setAdapter(null);
//                    getHandler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            mEditInput.setFocusable(false);
//                            mEditInput.setFocusableInTouchMode(false);
//                            mEditInput.clearFocus();
//                            Android.hideSoftInput(getContext());
//                        }
//                    }, 200);
//                }
//                break;
//        }
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 1203) {
            setResult(RESULT_OK, data);
            finish();
        } else if (resultCode == Activity.RESULT_OK && 1602 == requestCode) {
            setResult(RESULT_OK);
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
